<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Ctm_common extends CI_Controller {

	function __construct() {
		parent::__construct();
		$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
	}
	
	function index() {
		if ($this -> mod_users -> is_logged_in()) {
			redirect("users");
		} else {
			$this -> form_validation -> set_rules('email', 'lang:login_email', 'callback_login_check');
			$this -> form_validation -> set_error_delimiters('<div class="alert alert-danger" style="position:absolute;top:20px;text-align:center; width:92%;">', '</div>');

			if ($this -> form_validation -> run() == FALSE) {
				$this->load->view(B_AUTHOR.'login');
			} else {
				redirect("users");
			}
		}
	}

	function login_check($email) {
		$password = $this -> input -> post("password");

		if (!$this -> mod_users -> login($email, $password)) {
			$this -> form_validation -> set_message('login_check', $this -> lang -> line('login_invalid_email_and_password'));
			return false;
		}
		return true;
	}
	 public function logout(){
	 			$this->mod_users->logout();
	}
	 public function no_allow_access(){
	 	$module_id=$this->uri->segment(3);
	 	$data['title']="No allow access ".$module_id;
		$data['module']=$module_id;
	 	$this->load->view(B_MASTER.'index',$data);
	 }
	 public function no_allow_modify(){
	 	$module_id=$this->uri->segment(3);
	 	$data['title']="No allow modify ".$module_id;
		$data['module']=$module_id;
	 	$this->load->view(B_MASTER.'index',$data);
	 }
	 
	 public function change_password(){
	 	if (!$this -> mod_users -> is_logged_in()) {
			redirect('ctm_common');
		}
		
	 	$data['title']="Change Password";	
	 
	 	$this -> form_validation -> set_rules('password', 'Password', 'trim|min_length[4]|max_length[25|matches[confirm_password]');
		$this -> form_validation -> set_rules('confirm_password', 'Confirm passowrd', 'trim|min_length[4]|max_length[25]');
		
		if($this->form_validation->run()===FALSE){
			$this->load->view(B_MASTER.'index',$data);
		}else{
			$password=$this->input->post("password");
			$user_id=$this -> mod_users -> get_logged_in_user_info() -> user_id;
			
			$is_updated=$this->mod_globals->update(PREFIX_TABLE."users",array('password'=>md5($password)),array('user_id'=>$user_id));
			if($is_updated){
				$this->session->set_flashdata('success','Your password was changed success.');
				redirect('ctm-common/change-password');
			}else{
				$this->session->set_flashdata('error','Your password was changed fail.');
				redirect('ctm-common/change-password');	
			}
		}
	 }
	public function my_profile(){
		if (!$this -> mod_users -> is_logged_in()) {
			redirect('ctm_common');
		}
		
		$data['title']="My Profile";
		$user_id=$this -> mod_users -> get_logged_in_user_info() -> user_id;
		
		$this->form_validation->set_rules('username','lang:form_label_username','required');
		$this->form_validation->set_rules('email','lang:form_label_email','required|valid_email');
		$this->form_validation->set_rules('first_name','First Name','required');
		$this->form_validation->set_rules('last_name','Last Name','required');
		
		if($this->form_validation->run()===FALSE){
			
			$data['user_data']=$this->mod_globals->select_where(PREFIX_TABLE."users",array('user_id'=>$user_id));
			//$this->load->view('welcome_message',$data);
			$this->load->view(B_MASTER.'index',$data);
			
		}else{
			
			// Create users table
			$data_update_users=array(
				'username'=>$this->input->post('username'),
				'first_name'=>$this->input->post('first_name'),
				'last_name'=>$this->input->post('last_name'),
				'email'=>$this->input->post('email')
			);
			
			$is_updated=$this->mod_globals->update(PREFIX_TABLE."users",$data_update_users,array('user_id'=>$user_id));
			if($is_updated){
				$this->session->set_flashdata('success','Your profile  was updated success.');
				redirect('ctm-common/my-profile');
			}else{
				$this->session->set_flashdata('error','Your profile was updated fail.');
				redirect('ctm-common/my-profile');	
			}
			
		}
	}
}
